<template>
    <div class="block">
        <el-row class="block-item">
            url: <el-input type="textarea" rows="3" v-model="url" style="width:80%"></el-input>
            <el-button type="primary" @click="getAccountInfo">开始</el-button>
            
            <el-link target="_blank" type="primary" :href="url">打开界面</el-link>
        </el-row>
        <el-row class="block-item">
            <div class="block-title">用户信息:</div>
            <div class="ml20">
                <el-col :span="12">sid: {{userInfo.sid}}</el-col>
                <el-col :span="12">&nbsp;</el-col>
                <template v-if="userInfo.cardInfo">
                    <el-col :span="4">姓名: {{userInfo.cardInfo.name}}</el-col>
                    <el-col :span="4">卡号: {{userInfo.cardInfo.icCardNo}}</el-col>
                    <el-col :span="4">余额: {{userInfo.money}}</el-col>
                </template>
            </div>
        </el-row>
        <el-row class="block-item">
            <div class="block-title">
                医技预约&nbsp;&nbsp;&nbsp;&nbsp;
                <el-button @click="getOrderList" type="primary" size="mini">查询</el-button>
                &nbsp;&nbsp;&nbsp;&nbsp;
                自动查询<el-switch v-model="aotoQry" ></el-switch>
            </div>
            <div class="ml20">
                <el-row class="block-item" v-for="(item,idx) in orderList" :key="idx">
                    <div class="block-title">
                        日期: {{item.date}} &nbsp;&nbsp;&nbsp;&nbsp;{{item.labName}} 
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        最新查询时间: {{item.lastTime}}
                        <el-button @click="getDateList(item)" type="primary" size="mini">获取日期列表</el-button>
                        <!-- <el-link target="_blank" :href="getDateHref(item)">打开预约界面</el-link> -->
                    </div>
                    <template v-if="item.dateList">
                        <el-row class="block-item" v-for="(dateitem,idx1) in item.dateList" :key="idx1">
                            <div class="block-title">
                                预约日期: {{dateitem.bookDate}} - {{dateitem.weekDay}}
                                &nbsp;&nbsp;&nbsp;&nbsp;
                                最新查询时间: {{dateitem.lastTime}}
                                <el-button @click="getTimeList(dateitem)" type="primary" size="mini">获取时间列表</el-button>
                            </div>
                            <template v-if="dateitem.timeList">
                                <span v-for="(timeitem,idx2) in item.dateitem.timeList" :key="idx2">
                                    sourceId:{{timeitem.sourceId}} - time:{{timeitem.time}} - sequence:{{timeitem.sequence}} &nbsp;&nbsp;&nbsp;&nbsp;
                                </span>
                            </template>
                        </el-row>
                    </template>
                </el-row>

                <!-- 根据选择的时间进入下一步 -->
<!-- "&sourceId=" + $scope.sourceItem.sourceId + "&time=" + $scope.sourceItem.time + 
"&sequence=" + $scope.sourceItem.sequence +

https://3030.ij120.zoenet.cn/appointSure?patientType=1&icCardNo=G14602949&sourceId=&hisKeys=&selectRowID=&proName=%E5%BD%A9%E8%B6%85%EF%BC%9AI%E7%BA%A7%E4%BA%A7%E5%89%8D%E8%B6%85%E5%A3%B0%E6%A3%80%E6%9F%A5&&cardId=19900831fa1b40ac91f12b17db157bda&patientName=%E6%9E%97%E8%99%B9&time=15:00&sequence=11111111111111111 -->

            </div>
        </el-row>
    </div>
</template>


<script>
import config from '~/config'
import dayjs from '~/plugins/day';

import datadict from '~/plugins/dict';


export default {
    data() {
        return {
            tableheight: null,
            // url: "https://3030.ij120.zoenet.cn/org/orgIndexWX?platform=WX&title=%E8%87%AA%E5%8A%A9%E9%A2%84%E7%BA%A6&orgCode=123500004880031160&appCode=HC_FJSFY_WX&token=pf2493a310351b4314a24aae7588bab186&newOpenId=opiF-uJhAEWhaNfGJ8RylwLqL1wM&openId=opiF-uJhAEWhaNfGJ8RylwLqL1wM",
            url: "https://3030.ij120.zoenet.cn/org/orgIndexWX?platform=WX&title=%E8%87%AA%E5%8A%A9%E9%A2%84%E7%BA%A6&orgCode=123500004880031160&appCode=HC_FJSFY_WX&token=pf10098a149d914e6da38327f99e258ec9&newOpenId=opiF-uODYsHyvyQQJN0BQw4nWSBA&openId=opiF-uODYsHyvyQQJN0BQw4nWSBA",
            userInfo : {},
            orderList: [],
            aotoQry: false,
            
            orderUrl: "https://3030.ij120.zoenet.cn/api/mtReservation/getLaboratoryList",
            dateUrl : "https://3030.ij120.zoenet.cn/api/mtReservation/getScheduleDate",
            timeUrl : "https://3030.ij120.zoenet.cn/api/mtReservation/getLabSourceList",
        }
    },
     

    mounted() {
        // this.$axios.defaults.withCredentials=true
        this.setTimer();
    },

    methods: {
        setTimer(){
            var that = this;
            var autoTimer = setInterval(()=>{
                if(!that.aotoQry) return;
                that.qryTimerList();
            },1000);
            this.$once('hook:beforeDestroy', () => {            
                clearInterval(autoTimer);                                    
            })
        },
        qryTimerList(){
            var that = this;
            if(!this.preCheck()) return;
            if(!this.orderList || this.orderList.length==0) return;
            this.orderList.map((order)=>{
                if(!order.dateList || order.dateList.length==0){
                    // console.info(order.requestInfo);
                    that.getDateList(order);
                    return;
                }
                order.dateList.map((dateItem)=>{
                    // console.info(dateItem.bookDate);
                    that.getTimeList(dateItem);
                });
            });

        },
        getAccountInfo() {
            this.$axios.post(`${config.sapi}/api/fuyou/getAccountInfo`,{url:this.url})
            .then((res) => {
                if (res.data.code == 200) {
                    this.userInfo = res.data.data;
                } else {
                    this.$message.error(res.data.message);
                }
            })
        },
        preCheck(){
            if(!this.userInfo.cardInfo || !this.userInfo.cardInfo.icCardNo){
                this.$message.error("请先获取就诊卡信息");
                return false;
            }
            return true;
        },
        getOrderList(){
            if(!this.preCheck()) return;
            var url = this.orderUrl;
            var params = [];
            params.push({"name":"icCardNo","value":this.userInfo.cardInfo.icCardNo});
            params.push({"name":"patientType","value":"1"});
            this.publicReq(url,params,(resp)=>{
                this.orderList = resp.data;
            });
        },
        reRender(){
            var that = this;
            setTimeout(function(){
                that.orderList.push({});
                that.orderList.splice(that.orderList.length-1,1);
            },200);
        },
        getDateList(item){
            var that = this;
            if(!this.preCheck()) return;
            var url = this.dateUrl;
            var params = [];
            params.push({"name":"patientType","value":"1"});
            params.push({"name":"icCardNo","value":this.userInfo.cardInfo.icCardNo});
            // params.push({"name":"labCodeList","value":""});
            // params.push({"name":"labCode","value":""});
            // params.push({"name":"billDept","value":""});
            // params.push({"name":"hisKeys","value":""});
            params.push({"name":"patientInfo","value":item.patientInfo});
            params.push({"name":"requestInfo","value":item.requestInfo});

            item.lastTime = dayjs().format("YYYY-MM-DD HH:mm:ss");
            this.publicReq(url,params,(resp)=>{
                if(!resp.data){
                    this.$notify.error({ message: resp.message });
                    // this.$message.error(resp.message);
                    return;
                }
                if(resp.data.length>0){
                    item.dateList = resp.data;
                    item.dateList.splice(0,item.dateList.length-1);
                }else {
                    this.$message.error("未查询到符合条件的信息");
                }
                // this.orderList = resp.data;
            });
        },
        getTimeList(item){
            var that = this;
            if(!this.preCheck()) return;
            var url = this.timeUrl;
            var params = [];
            params.push({"name":"bookDate","value":item.bookDate});
            params.push({"name":"reservationResult","value":JSON.stringify(item.reservationResult)});
            params.push({"name":"schedName","value":"0"});
            params.push({"name":"patientType","value":"1"});
            params.push({"name":"icCardNo","value":this.userInfo.cardInfo.icCardNo});
            // params.push({"name":"hisKeys","value":""});
            // params.push({"name":"labCodeList","value":""});
            // params.push({"name":"labCode","value":""});
            // params.push({"name":"billDept","value":""});
            // params.push({"name":"hisKeys","value":""});
            item.lastTime = dayjs().format("YYYY-MM-DD HH:mm:ss");

            this.publicReq(url,params,(resp)=>{
                if(!resp.data){
                    this.$notify.error({ message: resp.message });
                    // this.$message.error(resp.message);
                    return;
                }
                if(resp.data.length>0){
                    item.timeList = resp.data;
                }
            });
        },
        publicReq(url,params,cb){
            var listStr = "[]";
            if(params){
                listStr = JSON.stringify(params);
            }
            this.$axios.post(`${config.sapi}/api/fuyou/commonReq`,{sid:this.userInfo.sid,url: url,listStr:listStr})
            .then((res) => {
                if (res.data.code == 200) {
                    if(cb){
                        cb(res.data.data);
                        this.reRender()
                    }
                } else {
                    this.$notify.error({
                        // title: '错误',
                        message: res.data.message
                    });
                    // this.$message.error(res.data.message);
                }
            })
        },
        publicReqFy(url,params,cb){
            var args = {};
            params.map((item)=>{
                args[item.name]=item.value;
            });
            
            var reqconfig={
                headers: {
                    'Cookie': 'connect.sid='+this.userInfo.sid
                },
                // withCredentials:true
            }
            this.$axios.post(url,args,reqconfig)
            .then((res) => {
                if (res.data.code == 200) {
                    debugger;
                    // if(cb){
                    //     cb(res.data.data);
                    //     this.reRender()
                    // }
                } else {
                    this.$message.error(res.data.message);
                }
            })
        },
        getDateHref(item){
            var url = "https://3030.ij120.zoenet.cn/medicalAppointment/appoint?icCardNo=";
            url+=this.userInfo.cardInfo.icCardNo;
            url+="&labCodeList=";
            url+="&patientType=1";
            url+="&labCode=";
            url+="&billDept=";
            url+="&labName="+item.labName;
            url+="&hisKeys=";
            url+="&hisKey=";
            url+="&cardId="+this.userInfo.cardInfo.cardId;
            url+="&patientName="+this.userInfo.cardInfo.patientName;
            url+="&billDeptName="+item.billDeptName;
            return url;
        }
    }
}
</script>

<style lang="less" scoped>
.block-item{
     border:1px lightgray dotted;
     margin:5px 20px;
     padding:10px;
    .el-col{
        margin-top: 10px;
    }
}
.block-title{
    font-size:  16px;
    font-weight: bold;
    padding-bottom: 10px;
}
.flexdiv {
    display: flex;
    justify-content: flex-start;
    margin-bottom: 10px;
    button{
        margin-left: 10px;
    }
}
.ml20 {
    margin-left: 20px;
}

.qryform{
    margin-bottom:10px;
    button{
        margin-top:0px;
        vertical-align: middle;
    }
}
.order-class{
    display: inline-block;
    margin-right: 10px;
    min-width: 50px;
}

.red{
    color: red;
}
.green{
    color: green;
}
.yellow{
    color: rgb(96, 22, 233)
}
.link-item{
    font-size: 12px;
}

</style>
